package test.fingers;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Bid extends Activity{
	private int bidSelected, suitSelected;
	private Spinner suitSpinner, bidSpinner;
	private EditText numCardsBidText;
	private TextView bidPrompt, statusText;
	private int numCardsBid = 5;
	private Button readyToBid;
	private ArrayAdapter<CharSequence> adapter, adapter2;
	private ImageView h1, h2, h3, h4, h5, h6, h7, h8, h9, h10;
	private Gameplay theGame;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gamelayout);
		
		//set up the bid dialog for pop up
		final Dialog bidDialog = new Dialog(Bid.this);
		bidDialog.setContentView(R.layout.biddialog);
		bidDialog.setTitle("Please choose your bid");
		bidDialog.setCancelable(true);

		//set up the spinner for bid type selection
		bidSpinner = (Spinner) bidDialog.findViewById(R.id.bidSpinner1);
		initBidSpinner();
		//set up spinner for suit selection
	    suitSpinner = (Spinner) bidDialog.findViewById(R.id.bidSpinner2);
	    initSuitSpinner();
	    //set up the bid amount box and prompt
	    bidPrompt = (TextView) bidDialog.findViewById(R.id.bidPrompt);
		numCardsBidText = (EditText) bidDialog.findViewById(R.id.bidTextBox);
		numCardsBidText.setOnKeyListener(new OnKeyListener(){
			public boolean onKey(View v, int keyCode, KeyEvent event){
				String str = numCardsBidText.getText().toString();
				if(str.equals(""))
					numCardsBid = -1;
				else
					numCardsBid = Integer.parseInt(str);

				// If the event is a key-down event on the "enter" button
				if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)){
					if(numCardsBid >= 1 && numCardsBid <= Gameplay.handSize){ 
						bidDialog.cancel();
						playGame();
						return true;
					}else{ //TODO add a notifcation/popup to say invalid input
						
					}
				}

				return false;
			}
		});

		
		//set up a click listener for the continue button
	    bidDialog.findViewById(R.id.bidDialogOK).setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				if(numCardsBid >= 1 && numCardsBid <= Gameplay.handSize){ 
					bidDialog.cancel();
					playGame();
				}else{ //TODO add a notifcation/popup to say invalid input
					
				}
			}
		});
		
	    //set up the status text behind the bid button
	    statusText = (TextView) findViewById(R.id.statusText);
	    
	    //set up the button to call the bid dialog
	    readyToBid = (Button) findViewById(R.id.readyToBidButton);
	    readyToBid.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				bidDialog.show();
			}
		});
	    
		initGame();
	}
	
	private void playGame(){
		readyToBid.setVisibility(Button.INVISIBLE);
		readyToBid.setEnabled(false);
		statusText.setVisibility(TextView.VISIBLE);
		statusText.setEnabled(true);
		statusText.setText("Bidding Finished");
		
		enableHand(true);
	}
	
	private void initGame(){
		String[] hand;
		theGame = new Gameplay();
		theGame.initGame();
		
		hand = theGame.getUsersHand();
		
		try {
			initHand(hand);
		} catch (Exception e) {}
		initPlayers();
		enableHand(false);
	}

	//this displays the main players hand
	private void initHand(String[] hand) throws IOException {
		AssetManager AM = getAssets();
		InputStream iStr;
		Bitmap bitmap;
		int handSize = Gameplay.handSize;
		
		h1 = (ImageView) findViewById(R.id.hand1);
		h2 = (ImageView) findViewById(R.id.hand2);
		h3 = (ImageView) findViewById(R.id.hand3);
		h4 = (ImageView) findViewById(R.id.hand4);
		h5 = (ImageView) findViewById(R.id.hand5);
		h6 = (ImageView) findViewById(R.id.hand6);
		h7 = (ImageView) findViewById(R.id.hand7);
		h8 = (ImageView) findViewById(R.id.hand8);
		h9 = (ImageView) findViewById(R.id.hand9);
		h10 = (ImageView) findViewById(R.id.hand10);
		
		if(handSize >= 1){
			iStr = AM.open(hand[0]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h1.setImageBitmap(bitmap);
			h1.setVisibility(ImageView.VISIBLE);
			h1.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card1");
					h1.setVisibility(ImageView.INVISIBLE); 
				}	
			});
		}
		if(handSize >= 2){
			iStr = AM.open(hand[1]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h2.setImageBitmap(bitmap);
			h2.setVisibility(ImageView.VISIBLE);
			h2.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card2");
					h2.setVisibility(ImageView.INVISIBLE);
				}	
			});
		}
		if(handSize >= 3){
			iStr = AM.open(hand[2]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h3.setImageBitmap(bitmap);
			h3.setVisibility(ImageView.VISIBLE);
			h3.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card3");
					h3.setVisibility(ImageView.INVISIBLE); 
				}	
			});
		}
		if(handSize >= 4){
			iStr = AM.open(hand[3]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h4.setImageBitmap(bitmap);
			h4.setVisibility(ImageView.VISIBLE);
			h4.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card4");
					h4.setVisibility(ImageView.INVISIBLE); 
				}	
			});	
		}
		if(handSize >= 5){
			iStr = AM.open(hand[4]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h5.setImageBitmap(bitmap);
			h5.setVisibility(ImageView.VISIBLE);
			h5.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card5");
					h5.setVisibility(ImageView.INVISIBLE); 
				}	
			});
		}
		if(handSize >= 6){
			iStr = AM.open(hand[5]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h6.setImageBitmap(bitmap);
			h6.setVisibility(ImageView.VISIBLE);
			h6.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card6");
					h6.setVisibility(ImageView.INVISIBLE); 
				}	
			});
		}
		if(handSize >= 7){
			iStr = AM.open(hand[6]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h7.setImageBitmap(bitmap);
			h7.setVisibility(ImageView.VISIBLE);
			h7.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card7");
					h7.setVisibility(ImageView.INVISIBLE); 
				}	
			});	
		}
		if(handSize >= 8){
			iStr = AM.open(hand[7]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h8.setImageBitmap(bitmap);
			h8.setVisibility(ImageView.VISIBLE);
			h8.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card8");
					h8.setVisibility(ImageView.INVISIBLE); 
				}	
			});
		}
		if(handSize >= 9){
			iStr = AM.open(hand[8]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h9.setImageBitmap(bitmap);
			h9.setVisibility(ImageView.VISIBLE);
			h9.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card9");
					h9.setVisibility(ImageView.INVISIBLE); 
				}	
			});	
		}
		if(handSize >= 10){
			iStr = AM.open(hand[9]+".png");
			bitmap = BitmapFactory.decodeStream(iStr);
			h10.setImageBitmap(bitmap);
			h10.setVisibility(ImageView.VISIBLE);
			h10.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					statusText.setText("card10");
					h10.setVisibility(ImageView.INVISIBLE); 
				}	
			});
		}
		
		//AM.close(); //NOTE: not sure if this needs to be included in some way, it doesnt work when enabled
	}
	
	private void enableHand(boolean enable){
		h1.setEnabled(enable);
		h2.setEnabled(enable);
		h3.setEnabled(enable);
		h4.setEnabled(enable);
		h5.setEnabled(enable);
		h6.setEnabled(enable);
		h7.setEnabled(enable);
		h8.setEnabled(enable);
		h9.setEnabled(enable);
		h10.setEnabled(enable);
	}
	
	//This displays the players if they are in the game
	private void initPlayers(){
		RelativeLayout playerB, playerC, playerD, playerE, playerF;
		int numPlayers = Gameplay.numPlayers;
		
		playerB = (RelativeLayout) findViewById(R.id.playerbLayout);
		playerC = (RelativeLayout) findViewById(R.id.playercLayout);
		playerD = (RelativeLayout) findViewById(R.id.playerdLayout);
		playerE = (RelativeLayout) findViewById(R.id.playereLayout);
		playerF = (RelativeLayout) findViewById(R.id.playerfLayout);
		
		if(numPlayers == 4 || numPlayers == 5 || numPlayers == 6){
			playerB.setVisibility(RelativeLayout.VISIBLE);
			playerF.setVisibility(RelativeLayout.VISIBLE);
		}
		if(numPlayers == 3 || numPlayers == 5 || numPlayers == 6){
			playerC.setVisibility(RelativeLayout.VISIBLE);
			playerE.setVisibility(RelativeLayout.VISIBLE);
		}
		if(numPlayers == 2 || numPlayers == 4 || numPlayers == 6){
			playerD.setVisibility(RelativeLayout.VISIBLE);
		}
	}

	private void initSuitSpinner() {
		adapter2 = ArrayAdapter.createFromResource(this, R.array.suitArray, android.R.layout.simple_spinner_item);
	    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    suitSpinner.setAdapter(adapter2);
	    //suit spinner listener
	    suitSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				suitSelected = pos;
			}

			public void onNothingSelected(AdapterView<?> arg0) {}
	    });
	}

	private void initBidSpinner() {
		adapter = ArrayAdapter.createFromResource(this, R.array.bidArray, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    bidSpinner.setAdapter(adapter);
	    bidSpinner.setSelection(3); //PASS by default
	    //bid spinner listener
	    bidSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				bidSelected = pos + 1;
				if(bidSelected == Gameplay.BID){
					viewSuitSel(true);
				}
				else{
					viewSuitSel(false);
				}
				
			}

			public void onNothingSelected(AdapterView<?> arg0) {}
	    });
	}
	
	private void viewSuitSel(boolean view){
		if(view){
			suitSpinner.setVisibility(Spinner.VISIBLE);
			suitSpinner.setEnabled(true);
			numCardsBidText.setVisibility(EditText.VISIBLE);
			numCardsBidText.setEnabled(true);
			bidPrompt.setVisibility(EditText.VISIBLE);
			bidPrompt.setEnabled(true);
		}else{
			suitSpinner.setVisibility(Spinner.INVISIBLE);
			suitSpinner.setEnabled(false);
			numCardsBidText.setVisibility(EditText.INVISIBLE);
			numCardsBidText.setEnabled(false);
			bidPrompt.setVisibility(EditText.INVISIBLE);
			bidPrompt.setEnabled(false);
		}
	}
}
